home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / flight / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.2 KB  |  335 lines

  1. /*
  2.  * Copyright 1983-1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*
  19.  *  flight/main.c $Revision: 1.15 $
  20.  */
  21.  
  22. #include "flight.h"
  23.  
  24.  
  25. main(argc, argv)
  26. {
  27.     flight(argc, argv);
  28. }
  29.  
  30.  
  31. reset_fov(nfov)
  32.     register int nfov;
  33. {
  34.     float sin, cos;
  35.  
  36.     fov = nfov;
  37.     if (hud || shadow)
  38.     ar = (xmaxwindow+1.0)/(ymaxwindow+1.0);
  39.     else
  40.     ar = (xmaxwindow-1.0)/(ymaxwindow-ymiddle);
  41.  
  42.     /*
  43.      *  calculate dist_for_lines
  44.      */
  45.     nfov >>= 1;        /* half field of view    */
  46.     gl_sincos(nfov, &sin, &cos);
  47.     dist_for_lines = 25 * 200/3 * cos/sin;
  48.     if (hud || shadow)
  49.     dist_for_lines *= 2;
  50.     dist_for_lines >>= 5;
  51.     dist_for_lines *= dist_for_lines;
  52.  
  53.     /*
  54.      *  calculate non rotated viewing frustum normals
  55.      */
  56.     frustum[0][0] = frustum[2][0] = 0.0;
  57.     frustum[0][1] = cos;
  58.     frustum[2][1] = -cos;
  59.     frustum[0][2] = frustum[2][2] = sin;
  60.     gl_sincos((int)(nfov * ar + 0.5), &sin, &cos);
  61.     frustum[1][0] = cos;
  62.     frustum[3][0] = -cos;
  63.     frustum[1][1] = frustum[3][1] = 0.0;
  64.     frustum[1][2] = frustum[3][2] = sin;
  65. }
  66.  
  67.  
  68.  
  69. #define DY 0.25
  70.  
  71. make_crash(msg)
  72.     char *msg;
  73. {
  74.     register Plane p;
  75.  
  76.     p = planes[0];        /* a bold assumption */
  77.  
  78.     if (p->status <= MEXPLODE)
  79.     return;
  80.  
  81. #ifdef AUDIO
  82.     play_samps(diesample, dielength);
  83. #endif
  84.  
  85.     p->lost++;            /* increment my lost count */
  86.     p->status = MEXPLODE;
  87.     trash_plane();
  88.  
  89.     clear_report();
  90.     add_report_line(50.0, 0.8, msg);    /* crash message */
  91.  
  92.     if (!hud)
  93.     {
  94.     frontbuffer(TRUE);
  95.     pushmatrix();
  96.     pushviewport();
  97.     clear_report_area();
  98.     draw_report();
  99.     popmatrix();
  100.     popviewport();
  101.     frontbuffer(FALSE);
  102.     }
  103. }
  104.  
  105.  
  106. clear_report_card()
  107. {
  108.     clear_report();
  109.     if (!hud && !shadow)
  110.     {
  111.     frontbuffer(TRUE);    /* clear report card from both buffers */
  112.     clear_report_area();
  113.     frontbuffer(FALSE);
  114.     }
  115. }
  116.  
  117.  
  118. int report_card(descent, roll, vx, vz, wheels, p)
  119.     int descent, vx, vz;
  120.     int roll, wheels;
  121.     register Plane p;
  122. {
  123.     short on_runway;
  124.     register int azimuth, rating;
  125.     register float y;
  126.     float xdist, zdist;
  127.     register char charbuf[80];
  128.  
  129.     azimuth = p->azimuth;
  130.     on_runway = IN_BOX(p, -100.0, 100.0, -8500.0, 0.0);
  131.  
  132.     roll /= 10;
  133.     if (roll > 180) roll -= 360;
  134.     rating = 1;
  135.  
  136.     clear_report();
  137.  
  138.     y = 0.8;
  139.  
  140.     add_report_line(50.0, y, "Landing Report Card:");
  141.  
  142.     sprintf(charbuf,"Rate of descent: %d fpm", descent * 60);
  143.     add_report_line(50.0, y-DY, charbuf);
  144.  
  145.     sprintf(charbuf, "Roll angle: %d", roll);
  146.     add_report_line(50.0, y-2*DY, charbuf);
  147.  
  148.     sprintf(charbuf, "Air speed: %d knots", vz);
  149.     add_report_line(50.0, y-3*DY, charbuf);
  150.  
  151.     if (!wheels)
  152.     {
  153.     add_report_line(450.0, y, "*** Landed with the landing gear up!");
  154.     rating = 0;
  155.     }
  156.     if (descent > 10)
  157.     {
  158.     add_report_line(350.0, y-DY, "*** Descending too fast!");
  159.     rating = 0;
  160.     }
  161.     if (roll < 0)
  162.     roll = -roll;
  163.     if (roll > 10)
  164.     {
  165.     add_report_line(350.0, y-2*DY, "*** Too much roll!");
  166.     rating = 0;
  167.     }
  168.     if (!on_runway)
  169.     {
  170.     sprintf(charbuf, "*** Landed off the runway!");
  171.     add_report_line(350.0, y-3*DY, charbuf);
  172.     rating = 0;
  173.     }
  174.     else if (vx > 10 || vx < -10)
  175.     {
  176.     sprintf(charbuf, "*** Too much drifting: %d fps", vx);
  177.     add_report_line(350.0, y-3*DY, charbuf);
  178.     rating = 0;
  179.     }
  180.     if (roll > 20 || descent > 20 || vx > 20 || vx < -20)
  181.     rating = -1;
  182.  
  183.     if (rating == 1)        /* good landing => rate it */
  184.     {
  185.     sprintf(charbuf, "Sideways speed: %d fps", vx);
  186.     add_report_line(650.0, y, charbuf);
  187.  
  188.     if (azimuth < 900 || azimuth > 2700)
  189.         zdist = -1075.0 - p->z;
  190.     else
  191.         zdist = -7425.0 - p->z;
  192.     xdist = fabs(p->x);
  193.  
  194.     sprintf(charbuf, "Distance from centerline: %d", (int)xdist);
  195.     add_report_line(650.0, y-DY, charbuf);
  196.  
  197.     zdist = fabs(zdist);
  198.     sprintf(charbuf, "Distance from touchdown: %d", (int)zdist);
  199.     add_report_line(650.0, y-2*DY, charbuf);
  200.  
  201.     if (azimuth > 2700)
  202.         azimuth = 3600-azimuth;
  203.     else if (azimuth > 900)
  204.         azimuth = 1800 - azimuth;
  205.     if (azimuth < 0)
  206.         azimuth = -azimuth;
  207.     azimuth /=10;
  208.     sprintf(charbuf, "Heading error: %d degrees", azimuth);
  209.     add_report_line(650.0, y-3*DY, charbuf);
  210.  
  211.     if (vx < 0)
  212.         vx = -vx;
  213.     rating = 100 - descent - roll - azimuth - (vx>>1)
  214.             -(int)(.01 * zdist) - (int)(.1 * xdist);
  215.     if (rating < 0)
  216.         rating = 0;
  217.     sprintf(charbuf, "Nice landing! (%d/100)", rating);
  218.     add_report_line(250.0, y, charbuf);
  219.     }
  220.     else if (rating == 0)
  221.     {
  222.     add_report_line(250.0, y, "CRASH LANDING! (0/100)");
  223.     }
  224.     else
  225.     {
  226.     add_report_line(250.0, y, "EXPLODED ON IMPACT!");
  227.     broadcast("exploded on impact");
  228.     }
  229.  
  230.     if (!hud)
  231.     {
  232.     frontbuffer(TRUE);
  233.     clear_report_area();
  234.     draw_report();
  235.     frontbuffer(FALSE);
  236.     }
  237.     return(rating);
  238. }
  239.  
  240. /* check my missile against other planes    */
  241. check_missile(p)
  242.     register Plane p;
  243. {
  244.     char buf[NAME_LENGTH+32];
  245.     register Plane ptest, *pp;
  246.     long last_kill;
  247.  
  248.     last_kill = p->mkill;
  249.     FOR_EACH_PLANE (ptest, pp)
  250.     if ((p != ptest || p->mtype == TYPE_SAM) && test_blow_up(p, ptest))
  251.     {
  252.         p->mkill = PLANE_ID(ptest);
  253.         if (last_kill == NULL_PLANE_ID)
  254.         {
  255.         p->mx = .2 * p->mx + .8 * ptest->x;
  256.         p->my = .2 * p->my + .8 * ptest->y;
  257.         p->mz = .2 * p->mz + .8 * ptest->z;
  258.         }
  259.         if (p->mkill != last_kill)    /* debounce */
  260.         {
  261.         extern char *WEAPON_NAME[];
  262.  
  263.         p->won++;
  264.         sprintf(buf, "destroyed %s with a %s",
  265.                 ptest->myname, WEAPON_NAME[p->mtype]);
  266.         broadcast(buf);
  267.         }
  268.         return;
  269.     }
  270. }
  271.  
  272.  
  273. int test_blow_up(m, p)
  274.     register Plane m, p;
  275. {
  276.     register int dx, dy, dz;
  277.     static int MDIST[] = {250, 350, 150, 300};
  278.  
  279.     /*
  280.      *  if the plane is not exploding
  281.      */
  282.     if (p->status > MEXPLODE)
  283.     {
  284.     dx = m->mx - p->x;
  285.     dy = m->my - p->y;
  286.     dz = m->mz - p->z;
  287.     if (dx < 0) dx = -dx;
  288.     if (dy < 0) dy = -dy;
  289.     if (dz < 0) dz = -dz;
  290.     if (dx + dy + dz < MDIST[m->mtype])
  291.     {
  292.         if (m->mstatus > MEXPLODE)
  293.         m->mstatus = MEXPLODE;
  294.         return (TRUE);
  295.     }
  296.     }
  297.     return(FALSE);
  298. }
  299.  
  300. /* find and return the closest plane to me    */
  301. Plane find_closest_plane(myp)
  302.     register Plane myp;
  303. {
  304.     float myx, myy, myz;
  305.     float dx, dy, dz, d, dbest;
  306.     register Plane p, *pp, pbest;
  307.  
  308.     pbest = NULL;
  309.     dbest = 1e30;
  310.     myx = my_ptw[2][0];
  311.     myy = my_ptw[2][1];
  312.     myz = my_ptw[2][2];
  313.  
  314.     FOR_EACH_PLANE(p, pp)        /* for each plane    */
  315.     /* if its not me, not exploding, above 150 feet, not C150    */
  316.     if ((p != myp || myp->mtype == TYPE_SAM) && p->status > MEXPLODE &&
  317.     p->y > 150.0 && p->type != C150)
  318.     {
  319.     dx = myp->x - p->x;        /* compute distance    */
  320.     dy = myp->y - p->y;
  321.     dz = myp->z - p->z;
  322.     d = sqrt(dx*dx + dy*dy + dz*dz);
  323.  
  324.     if ((myx*dx + myy*dy + myz*dz)/d > .988)
  325.     {
  326.         if (d < dbest)        /* and compare with best */
  327.         {
  328.         dbest = d;
  329.         pbest = p;
  330.         }
  331.     }
  332.     }
  333.     return(pbest);
  334. }
  335.